package com.example.t410.service.serviceImpl;

import com.example.t410.dao.Result;
import com.example.t410.dao.Topic;
import com.example.t410.model.AnsBasic;
import com.example.t410.model.AnsReturn;
import com.example.t410.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {


    @Autowired
    private MongoTemplate mongoTemplate;

    //查全部
    @Override
    public List<Topic> findAll() {
        return mongoTemplate.findAll(Topic.class);
    }

    //计算
    public AnsReturn calculateAns(String string) {
        StringBuffer stringBuffer = new StringBuffer(string);
        HashMap<Character, Integer> map = new HashMap<>();

        for (int i = 0; i < stringBuffer.length(); i++) {
            char a = stringBuffer.charAt(i);
            if (map.get(a) == null) {
                map.put(a, 1);
            } else {
                map.put(a, map.get(a) + 1);
            }
        }
        System.out.println(map.keySet());
        System.out.println(map.values());

        Integer e = (map.get('E') == null ? 0 : map.get('E'));
        Integer i = (map.get('I') == null ? 0 : map.get('I'));
        Integer s = (map.get('S') == null ? 0 : map.get('S'));
        Integer n = (map.get('N') == null ? 0 : map.get('N'));
        Integer t = (map.get('T') == null ? 0 : map.get('T'));
        Integer f = (map.get('F') == null ? 0 : map.get('F'));
        Integer j = (map.get('J') == null ? 0 : map.get('J'));
        Integer p = (map.get('P') == null ? 0 : map.get('P'));


        //计算比率
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String eRate = decimalFormat.format(((double) e / stringBuffer.length()) * 100);
        String iRate = decimalFormat.format(((double) i / stringBuffer.length()) * 100);
        String sRate = decimalFormat.format(((double) s / stringBuffer.length()) * 100);
        String nRate = decimalFormat.format(((double) n / stringBuffer.length()) * 100);
        String tRate = decimalFormat.format(((double) t / stringBuffer.length()) * 100);
        String fRate = decimalFormat.format(((double) f / stringBuffer.length()) * 100);
        String jRate = decimalFormat.format(((double) j / stringBuffer.length()) * 100);
        String pRate = decimalFormat.format(((double) p / stringBuffer.length()) * 100);

        //拼接结果
        StringBuffer res = new StringBuffer();
        res = e > i ? res.append("E") : res.append('I');
        res = s > n ? res.append('S') : res.append('N');
        res = t > f ? res.append('T') : res.append('F');
        res = j > p ? res.append('J') : res.append('P');


        Query query = new Query();
        query.addCriteria(Criteria.where("ansCode").is(res.toString()));//从字典表中获取性格结果信息
        Result result = mongoTemplate.findOne(query, Result.class);

        AnsReturn ans = new AnsReturn(res.toString(), eRate, iRate, sRate, nRate, tRate, fRate, jRate, pRate, result.getAnsName(), result.getAnsDisc());

        return ans;
    }
}